package org.example.mq.demo;

import org.example.mq.mqclient.Channel;
import org.example.mq.mqclient.Connection;
import org.example.mq.mqclient.ConnectionFactory;
import org.example.mq.mqserver.core.BasicProperties;
import org.example.mq.mqserver.core.Consumer;
import org.example.mq.mqserver.core.ExchangeType;

import java.io.IOException;

public class DemoConsumer {
    public static void main(String[] args) throws IOException {
        System.out.println("消费者启动！");

        ConnectionFactory connectionFactory  = new ConnectionFactory();

        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare("testQueue",true,false,false,null);
        channel.exchangeDeclare("testDirectExchange", ExchangeType.DIRECT,true,false,null);

        channel.basicConsume("testQueue", true, new Consumer() {
            @Override
            public void handleDelivery(String consumerTag, BasicProperties basicProperties, byte[] body) throws IOException {
                System.out.println("[消费数据开始！]");
                System.out.println("consumerTag:"+consumerTag);
                String message = new String(body);
                System.out.println("message:"+message);
                System.out.println("消费数据结束了！");
            }
        });
    }
}
